package com.pv.user.controller;

import cn.hutool.core.util.PhoneUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.pv.common.utils.R;
import com.pv.user.common.RedisConstant;
import com.pv.user.entity.UserEntity;
import com.pv.user.feign.ThirdPartyFeignService;
import com.pv.user.service.SmsService;
import com.pv.user.service.UserService;
import com.pv.user.vo.User;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @Description:
 * @Author: Zk
 * @Date: 2021/8/23 9:20
 */
@Slf4j
@RestController
@RequestMapping("user/login")
public class LoginController {
    @Autowired
    private UserService userService;
    @Autowired
    private SmsService smsService;

    /**
     * 根据用户名获取用户信息
     */
    @RequestMapping("/getByUsername/{username}")
    @ApiOperation(value="根据用户名获取用户信息",httpMethod="POST")
    public R getByUsername(@PathVariable("username") String username){
        UserEntity userEntity = userService.getOne(Wrappers.<UserEntity>lambdaQuery().eq(UserEntity::getUsername, username));
        return R.ok().setData(userEntity);
    }
    /**
     * 注册
     */
    @RequestMapping("/register")
    @ApiOperation(value="注册",httpMethod="POST")
    public R register(@RequestBody User.RegisterVo registerVo){
        String username = registerVo.getUsername();
        String password = registerVo.getPassword();
        if (username==null||password==null){
            return R.error("账号密码不能为空");
        }
        if (!smsService.matcheCodeAndGetPhone(registerVo.getUsername(),registerVo.getCode())){
            return R.error("验证码错误");
        }else {
            UserEntity one = userService.getOne(new LambdaQueryWrapper<UserEntity>().eq(UserEntity::getUsername, registerVo.getUsername()));
            if (one != null){
                return R.error("手机号已注册");
            }else {
                UserEntity userEntity = new UserEntity();
                userEntity.setUsername(registerVo.getUsername());
                userEntity.setPassword(new BCryptPasswordEncoder().encode(registerVo.getPassword()));
                userEntity.setEnabled(false);
                userEntity.setIsAdmin(0);
                userEntity.setIsPartner(0);
                userEntity.setHeadImgUrl("--------");
                boolean save = userService.save(userEntity);
                if (save){
                    return R.ok("成功");
                }else {
                    return R.error("请稍后重试");
                }
            }
        }
    }




}
